Pandas DataFrame Floats转Strings用法及代码示例 您所在的位置:网站首页 python float转str保留4位 Pandas DataFrame Floats转Strings用法及代码示例

Pandas DataFrame Floats转Strings用法及代码示例

2023-04-28 07:28| 来源: 网络整理| 查看: 265

在这篇文章中,我们将看到在 Pandas Dataframe 中将浮点数转换为字符串的不同方法? Pandas Dataframe 提供了更改列值数据类型的自由。我们可以将它们从 Integers 更改为 Float 类型,Integer 更改为 String,String 更改为 Integer,Float 更改为 String 等。

将浮点数转换为字符串的三种方法:

方法一:使用DataFrame.astype()。

用法:

DataFrame.astype(dtype, copy=True, errors=’raise’, **kwargs)

这用于将 pandas 对象转换为指定的 dtype。此函数还提供将任何合适的现有列转换为分类类型的函数。

范例1:将一列从浮点数转换为字符串。

Python3 # Import pandas library  import pandas as pd     # initialize list of lists  data = [['Harvey', 10, 45.25], ['Carson', 15, 54.85],         ['juli', 14, 87.21], ['Ricky', 20, 45.23],         ['Gregory', 21, 77.25], ['Jessie', 16, 95.21]]     # Create the pandas DataFrame  df = pd.DataFrame(data, columns = ['Name', 'Age', 'Marks'],                   index = ['a', 'b', 'c', 'd', 'e', 'f'])     # lets find out the data type  # of 'Marks' column print (df.dtypes)

输出:

现在,我们将“Marks”列的数据类型从 ‘float64’ 更改为 ‘object’。

Python3 # Now we will convert it from  # 'float' to 'String' type.  df['Marks'] = df['Marks'].astype(str)    print()    # lets find out the data # type after changing print(df.dtypes)    # print dataframe.  df 

输出:

范例2:将多于一列从浮点数转换为字符串。

Python3 # Import pandas library  import pandas as pd     # initialize list of lists  data = [['Harvey.', 10.5, 45.25, 95.2], ['Carson', 15.2, 54.85, 50.8],          ['juli', 14.9, 87.21, 60.4], ['Ricky', 20.3, 45.23, 99.5],         ['Gregory', 21.1, 77.25, 90.9], ['Jessie', 16.4, 95.21, 10.85]]     # Create the pandas DataFrame  df = pd.DataFrame(data, columns = ['Name', 'Age', 'Marks', 'Accuracy'],                   index = ['a', 'b', 'c', 'd', 'e', 'f'])     # lets find out the data type  # of 'Age' and 'Accuracy' columns print (df.dtypes)

输出:

现在,我们将“Accuracy”和“Age”列的数据类型从 ‘float64’ 更改为 ‘object’。

Python3 # Now Pass a dictionary to  # astype() function which contains  # two columns and hence convert them # from float to string type df = df.astype({"Age":'str', "Accuracy":'str'}) print()    # lets find out the data  # type after changing print(df.dtypes)    # print dataframe.  df 

输出:

方法二:使用Series.apply()。

用法:

DataFrame.apply(func, axis=0, raw=False, result_type=None, args=(), **kwds)

此方法允许用户传递一个函数并将其应用于 Pandas 系列的每个值。

例:将 DataFrame 的列从浮点数转换为字符串。

Python3 # Import pandas library  import pandas as pd     # initialize list of lists  data = [['Harvey.', 10.5, 45.25, 95.2], ['Carson', 15.2, 54.85, 50.8],         ['juli', 14.9, 87.21, 60.4], ['Ricky', 20.3, 45.23, 99.5],         ['Gregory', 21.1, 77.25, 90.9], ['Jessie', 16.4, 95.21, 10.85]]     # Create the pandas DataFrame  df = pd.DataFrame(data, columns = ['Name', 'Age', 'Percentage', 'Accuracy'],                   index = ['a', 'b', 'c', 'd', 'e', 'f'])     # lets find out the data  # type of 'Percentage' column print (df.dtypes)

输出:

现在,我们将“百分比”列的数据类型从“float64”更改为“对象”。

Python3 # Now we will convert it from  # 'float' to 'string' type.  df['Percentage'] = df['Percentage'].apply(str)  print()    # lets find out the data # type after changing print(df.dtypes)    # print dataframe.  df

输出:

方法3:使用Series.map()。

用法:

Series.map(arg, na_action=None)

此方法用于映射来自具有相同列的两个系列的值。

例:将 DataFrame 的列从浮点数转换为字符串。

Python3 # Import pandas library  import pandas as pd     # initialize list of lists  data = [['Harvey.', 10.5, 45.25, 95.2], ['Carson', 15.2, 54.85, 50.8],          ['juli', 14.9, 87.21, 60.4], ['Ricky', 20.3, 45.23, 99.5],         ['Gregory', 21.1, 77.25, 90.9], ['Jessie', 16.4, 95.21, 10.85]]     # Create the pandas DataFrame  df = pd.DataFrame(data, columns = ['Name', 'Age', 'Percentage', 'Accuracy'],                   index = ['a', 'b', 'c', 'd', 'e', 'f'])     # lets find out the data # type of 'Age' column print (df.dtypes)

输出:

现在,我们将“Age”列的数据类型从“float64”更改为“object”。

Python3 # Now we will convert it from 'float' to 'string' type.  # using DataFrame.map(str) function df['Age'] = df['Age'].map(str)   print()    # lets find out the data type after changing print(df.dtypes)    # print dataframe.  df 

输出:



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有